<?php

namespace app\index\service;

/**
 * Description of Cate
 * 分类管理 服务层
 * @author Dongasai
 */
class Cate
{

    /**
     * 删除分类
     * @param type $uid 发起用户
     * @param type $cate_id 分类id
     */
    public static function del($uid, $cate_id)
    {
        $cateModel = \think\Loader::model("cate");
        //验证用户是否存在权限
        //等待添加功能
        //验证是否存在父级分类
        $info      = self::info($cate_id);
        if (!$info) {
            return "不存在的数据!";
        }
        $p_info = $cateModel->get(['pid' => $cate_id]);
        if ($p_info) {
            return "存在子集分类!";
        }
        $where = [
            'id' => $cate_id
        ];
        //使用事务
        \think\Db::startTrans();
        $re    = \app\index\model\Cate::destroy($where);
        if ($re === 1) {
            \think\Db::commit();
            return true;
        } else {
            \think\Db::rollback();
            return "删除出错!;";
        }
    }

    /**
     * 获取分页的列表
     * @param type $uid 用户
     * @param type $group_id 分组
     * @param type $page 当前页数
     * @param type $low 分页条数
     */
    public static function listForPage($uid, $group_id = 0, $order = '', $page = 1, $low = 10)
    {
        $cateModel = \think\Loader::model("cate");
        if ($group_id) {
            $where = [
                'group_id' => $group_id
            ];
        } else {
            $group_id_list = Group::idList($uid);
            $where         = [
                'group_id' => ['in', $group_id_list]
            ];
        }


        $totle = $cateModel->where($where)->count();
        $list  = $cateModel->where($where)->order($order)->page($page, $low)->select();
        return ['totle' => $totle, 'list' => $list];
    }

    /**
     * 编辑分类
     * @param type $uid
     * @param type $to_id
     * @param type $data
     */
    public static function edit($uid, $to_id, $data)
    {
        //验证是否有权限更改
        //等待增加该功能
        $p_info     = self::info($data['pid']);
        $data['lv'] = $p_info['lv'] + 1;
        $cateModel  = \think\Loader::model("cate");

        $where = [
            'id' => $to_id
        ];
        $re    = $cateModel->save($data, $where);
        if ($re) {
            return TRUE;
        } else {
            return "数据库失败!";
        }
    }

    /**
     * 增加分类
     * @param type $uid
     * @param type $data
     */
    public static function add($uid, $data)
    {
        $p_info     = self::info($data['pid']);
        $data['lv'] = $p_info['lv'] + 1;
        $cateModel  = \think\Loader::model("cate");
        dump($data);
        $re         = $cateModel->data($data)->save();
        if ($re) {
            return TRUE;
        } else {
            return "数据库失败!";
        }
    }

    public static function info($id)
    {
        $cateModel = \think\Loader::model("cate");
        return $cateModel->get(['id' => $id]);
    }

    /**
     * 获取无限极分类的列表
     * @param type $group
     */
    public static function getRecursionList($group, $type = 1)
    {
        $list = self::getList($group);

        return \Org\util\Arr::recursion($list, 'pid', 'id', 0, 0, $type);
    }

    public static function getList($group, $pid = false)
    {
        $where = [
            'group_id' => $group
        ];
        if (is_numeric($pid)) {
            $where['pid'] = $pid;
        }
        $cateModel = \think\Loader::model("cate");
        $list      = $cateModel->all($where);
        return $list;
    }

}
